<template>
  <div id="app">
    <el-container class="app-container">
      <!-- 头部导航 -->
      <el-header class="app-header">
        <div class="header-content">
          <div class="logo">
            <el-icon class="logo-icon"><TrendCharts /></el-icon>
            <h1>加密货币爆仓监控</h1>
          </div>
          
          <div class="header-stats">
            <el-tag v-if="systemStats" type="info" size="large">
              监控币种: {{ systemStats.total_coins }}
            </el-tag>
            <el-tag v-if="systemStats" type="success" size="large">
              数据记录: {{ formatNumber(systemStats.total_records) }}
            </el-tag>
            <el-tag :type="connectionStatus ? 'success' : 'danger'" size="large">
              {{ connectionStatus ? '已连接' : '未连接' }}
            </el-tag>
          </div>
        </div>
      </el-header>
      
      <!-- 主要内容区域 -->
      <el-main class="app-main">
        <router-view />
      </el-main>
      
      <!-- 底部信息 -->
      <el-footer class="app-footer">
        <div class="footer-content">
          <span>© 2024 加密货币爆仓数据监控系统</span>
          <span v-if="lastUpdate">最后更新: {{ formatTime(lastUpdate) }}</span>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { TrendCharts } from '@element-plus/icons-vue'
import { useSystemStore } from '@/stores/system'
import { formatNumber, formatTime } from '@/utils/format'

const systemStore = useSystemStore()

const systemStats = ref(null)
const connectionStatus = ref(false)
const lastUpdate = ref(null)

let statusInterval = null

// 获取系统状态
const fetchSystemStatus = async () => {
  try {
    const stats = await systemStore.getSystemStats()
    systemStats.value = stats
    
    // 检查连接状态
    const health = await systemStore.getHealthStatus()
    connectionStatus.value = health.websocket_connected
    lastUpdate.value = Date.now()
  } catch (error) {
    console.error('获取系统状态失败:', error)
    connectionStatus.value = false
  }
}

onMounted(() => {
  // 初始加载
  fetchSystemStatus()
  
  // 定期更新状态
  statusInterval = setInterval(fetchSystemStatus, 10000) // 每10秒更新一次
})

onUnmounted(() => {
  if (statusInterval) {
    clearInterval(statusInterval)
  }
})
</script>

<style lang="scss" scoped>
.app-container {
  min-height: 100vh;
  background: #f7f8fa;
}

.app-header {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid #e4e7ed;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  
  .header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
  }
  
  .logo {
    display: flex;
    align-items: center;
    gap: 12px;
    
    .logo-icon {
      font-size: 32px;
      color: #409eff;
    }
    
    h1 {
      font-size: 24px;
      font-weight: 600;
      color: #303133;
      margin: 0;
    }
  }
  
  .header-stats {
    display: flex;
    gap: 12px;
    align-items: center;
  }
}

.app-main {
  padding: 20px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

.app-footer {
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-top: 1px solid #e4e7ed;
  height: 50px;
  
  .footer-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 100%;
    max-width: 1400px;
    margin: 0 auto;
    padding: 0 20px;
    font-size: 14px;
    color: #909399;
  }
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    gap: 10px;
    padding: 10px 20px;
  }
  
  .logo h1 {
    font-size: 20px;
  }
  
  .header-stats {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .app-main {
    padding: 10px;
  }
  
  .footer-content {
    flex-direction: column;
    gap: 5px;
    font-size: 12px;
  }
}
</style>